package com.example.exercise.analysis.datagenerator.service.impl;

import com.example.exercise.core.util.DateUtil;
import com.example.exercise.analysis.datagenerator.mapper.AppRecordMapper;
import com.example.exercise.analysis.datagenerator.service.AppInfos;
import com.example.exercise.analysis.datagenerator.service.AppStartTestService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * 添加测试数据
 */
@Service
public class AppStartTestServiceImpl implements AppStartTestService {

    private Logger LOGGER = LoggerFactory.getLogger(AppStartTestServiceImpl.class);

    @Resource
    private AppRecordMapper appRecordMapper;

    @Override
    public void truncateTable(){
        appRecordMapper.truncateTable("new_record");
        appRecordMapper.truncateTable("start_record");
    }

    @Override
    public void insertDataOrigin() {
        for (int i = 360; i >=0 ; i--) {
            appRecordMapper.insertStartDataOrigin(AppInfos.getNewDevice(360,i));
            String dateDayStr = DateUtil.getDateStr(i);
            for (int j = Math.min(i + 90, 360); j >= i; j--) {
                String dateStr = DateUtil.getDateStr(j);
                int size;
                if(j > i + 80){
                    size = new Random().nextInt(50);
                }else if(j <= i + 80 && j > i + 70){
                    size = new Random().nextInt(200);
                }else if(j <= i + 70 && j > i + 60){
                    size = new Random().nextInt(300);
                }else if(j <= i + 60 && j > i + 50){
                    size = new Random().nextInt(400);
                }else if(j <= i + 50 && j > i + 40){
                    size = new Random().nextInt(800);
                }else if(j <= i + 40 && j > i + 30){
                    size = new Random().nextInt(1000);
                }else if(j <= i + 30 && j > i + 20){
                    size = new Random().nextInt(1500);
                }else if(j <= i + 20 && j > i + 10){
                    size = new Random().nextInt(2000);
                }else{
                    size = new Random().nextInt(3000);
                }
                appRecordMapper.insertStartDataBefore(dateDayStr, dateStr, size);
            }
            LOGGER.info("已添加"+i+"天前的数据到原始记录表成功");
        }
        appRecordMapper.insertStartFromNew();
    }

}
